{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccf7b81a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import libraries\n",
    "import numpy as np\n",
    "import yfinance as yf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68118f93",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Use the `yfinance` library to download the front month S&P500 futures price data.\n",
    "sp500_prices = yf.download('ES=F')['Adj Close']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "724dd554",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate the daily logarithmic returns of the futures prices.\n",
    "log_returns = np.log(sp500_prices).diff()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7f24f8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate the standard deviation (volatility) of the logarithmic returns\n",
    "volatility = log_returns.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73b3df3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Annualize the volatility\n",
    "# There are approximately 252 trading days in a year\n",
    "annualized_volatility = volatility * np.sqrt(252)\n",
    "print()\n",
    "print(f\"annualized_volatility: {np.round(annualized_volatility * 100, 2)}%\")\n",
    "print()"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
